package chapter1.deepCopy.src.com.kkb.tz.test;

import java.io.*;

/**
 * @author : wushikai
 * <p>
 * date : 2021-02-18
 * 通过序列化实现深拷贝
 */
public class DeepCopyBySerialization {

    public static void main(String[] args) throws IOException, ClassNotFoundException {


        Person p = new Person("jetty", 22);
        p.setAddress("guangdong", "guangzhou");
        //通过序列化方法实现深拷贝
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bos);
        oos.writeObject(p);
        oos.flush();
        ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
        Person pc = (Person)ois.readObject();
        pc.setName("tomcat");
        System.out.println("p = " + p + "  " + p.print());
        System.out.println("pc = " + pc + "  " + pc.print());

    }




}
